package com.hengtongxing.hejiayun_employee.homepage;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.base.BaseFragment;
import com.hengtongxing.hejiayun_employee.base.DataCallBack;
import com.hengtongxing.hejiayun_employee.bean.BaseResponseBean;
import com.hengtongxing.hejiayun_employee.bean.OrderListBean;
import com.hengtongxing.hejiayun_employee.bean.TaskListBean;
import com.hengtongxing.hejiayun_employee.event.OrderRefushEvent;
import com.hengtongxing.hejiayun_employee.homepage.adapter.AdvisoryListAdapter;
import com.hengtongxing.hejiayun_employee.homepage.adapter.ShopOrderListAdapter;
import com.hengtongxing.hejiayun_employee.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun_employee.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun_employee.utils.CallPhoneUtil;
import com.hengtongxing.hejiayun_employee.utils.ToastUtil;
import com.hengtongxing.hejiayun_employee.widget.CommonNoTitleDialog;
import com.hengtongxing.hejiayun_employee.widget.WrapContentLinearLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 首页
 */
public class ShopOrderFragment extends BaseFragment implements OnRefreshLoadMoreListener {

    public static final String ORDER_TITLE_NAME = "title";
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String titleName;
    private String order_status;
    private int page = 1;
    private List<OrderListBean.DataBean.ItemListBean> beanList;
    private ShopOrderListAdapter listAdapter;
    private SkeletonScreen skeletonScreen;
    private IHomePageModel homePageModel;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_shoporder, container, false);
        unbinder = ButterKnife.bind(this, view);
        Bundle bundle = getArguments();
        registerEventBus();
        if (bundle != null) {
            titleName = bundle.getString(ORDER_TITLE_NAME);
            order_status = getType();
        }
        initView();
        initData();
        return view;
    }

    @Subscribe
    public void updata(OrderRefushEvent event) {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }

    private void initView() {
        homePageModel = new HomePageModelImpl();
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private void initData() {
        beanList = new ArrayList<>();
        listAdapter = new ShopOrderListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            OrderListBean.DataBean.ItemListBean itemListBean = beanList.get(position);
            if (type == 1) {
                String tel = beanList.get(position).getAgent_address().getMobile();
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否拨打" + tel);
                dialogBuild.setPositiveButton("是", v -> {
                    dialogBuild.dismiss();
                    CallPhoneUtil.callPhone(mActivity, tel);
                });
                dialogBuild.setNegativeButton("否", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            }
            if (type == 2) {
                if (itemListBean.getOrder_status() == 2) {
                    requestMeetOrder(beanList.get(position).getId() + "");
                }
            }
        });
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_product_list)
                .show();
        requestData();
    }

    public void requestMeetOrder(String id) {
        homePageModel.requestShopOrderMeetOrder(id, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("接单成功");
                EventBus.getDefault().post(new OrderRefushEvent());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    public void requestData() {
        homePageModel.requestHousesShopOrderIndex(page, order_status, new DataCallBack<OrderListBean>() {
            @Override
            public void onSuccessful(OrderListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        page++;
        requestData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 1;
        beanList.clear();
        requestData();
    }

    private String getType() {
        String status = "0";
        switch (titleName) {
            case "待配货":
                status = "2";
                break;
            case "已配货":
                status = "3";
                break;
        }
        return status;
    }
}
